<template>
  <div class="min-h-screen bg-gray-50 p-6">
    <div class="max-w-7xl mx-auto bg-white rounded-lg shadow-sm">
      <!-- 顶部操作区 -->
      <div class="p-6 flex justify-between items-center border-b">
        <el-button type="primary" class="!rounded-button" @click="handleAdd">
          <i class="el-icon-plus mr-1"></i>添加违禁词
        </el-button>
        <div class="flex items-center gap-4">
          <div class="relative">
            <el-input
              v-model="searchKeyword"
              placeholder="请输入关键词搜索"
              class="w-64"
              prefix-icon="el-icon-search"
            />
          </div>
          <el-button class="!rounded-button" @click="handleImport">
            <i class="el-icon-upload mr-1"></i>批量导入
          </el-button>
          <el-button class="!rounded-button" @click="handleExport">
            <i class="el-icon-download mr-1"></i>导出
          </el-button>
        </div>
      </div>

      <!-- 数据表格 -->
      <div class="p-6">
        <el-table :data="tableData" style="width: 100%;">
          <!-- 违禁词列 -->
          <el-table-column prop="word" label="违禁词"/>
          <!-- 替换方式列 -->
          <el-table-column prop="replaceType" label="替换方式">
            <template slot-scope="{ row }">
              <el-tag :type="getTagType(row.replaceType)">{{ row.replaceType }}</el-tag>
            </template>
          </el-table-column>
          <!-- 替换词列 -->
          <el-table-column prop="replacement" label="替换词"/>
          <!-- 添加时间列 -->
          <el-table-column prop="createTime" label="添加时间" width="180" />
          <!-- 操作列 -->
          <el-table-column label="操作" width="200" align="right">
            <template slot-scope="{ row }">
              <el-tooltip content="编辑" placement="top">
                <el-button class="!rounded-button whitespace-nowrap" @click="handleEdit(scope.row)">
                  <i class="el-icon-edit"></i>
                </el-button>
              </el-tooltip>
              <el-tooltip content="删除" placement="top">
                <el-button class="!rounded-button whitespace-nowrap" @click="handleDelete(scope.row)">
                  <i class="el-icon-delete"></i>
                </el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="flex justify-end mt-4">
          <el-pagination
            :current-page.sync="currentPage"
            :page-size.sync="pageSize"
            :page-sizes="[10, 20, 30, 50]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
          />
        </div>
      </div>
    </div>

    <!-- 编辑弹窗 -->
    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType === 'add' ? '添加违禁词' : '编辑违禁词'"
      width="500px"
    >
      <el-form :model="form" label-width="100px">
        <el-form-item label="违禁词">
          <el-input v-model="form.word" placeholder="请输入违禁词" />
        </el-form-item>
        <el-form-item label="替换方式">
          <el-select v-model="form.replaceType" placeholder="请选择替换方式" class="w-full">
            <el-option label="完全替换" value="完全替换" />
            <el-option label="部分替换" value="部分替换" />
            <el-option label="正则替换" value="正则替换" />
          </el-select>
        </el-form-item>
        <el-form-item label="替换词">
          <el-input v-model="form.replacement" placeholder="请输入替换词" />
        </el-form-item>
      </el-form>
      <template slot="footer">
        <span class="dialog-footer" style="display: flex; justify-content: flex-end; gap: 12px;">
          <el-button class="!rounded-button" @click="dialogVisible = false">取消</el-button>
          <el-button class="!rounded-button" type="primary" @click="handleSubmit">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchKeyword: '',
      currentPage: 1,
      pageSize: 10,
      total: 100,
      dialogVisible: false,
      dialogType: 'add', // 'add' 或 'edit'
      form: {
        id: 0,
        word: '',
        replaceType: '',
        replacement: ''
      },
      tableData: [
        {
          id: 1,
          word: '敏感词汇一',
          replaceType: '完全替换',
          replacement: '****',
          createTime: '2024-01-15 10:30:00'
        },
        {
          id: 2,
          word: '敏感词汇二',
          replaceType: '部分替换',
          replacement: '***',
          createTime: '2024-01-15 11:20:00'
        },
        {
          id: 3,
          word: '敏感词汇三',
          replaceType: '正则替换',
          replacement: '#####',
          createTime: '2024-01-15 14:15:00'
        }
      ]
    };
  },
  methods: {
    getTagType(type) {
      const typeMap = {
        '完全替换': 'success',
        '部分替换': 'warning',
        '正则替换': 'info'
      };
      return typeMap[type] || 'info';
    },
    handleAdd() {
      this.dialogType = 'add';
      this.form = {
        id: 0,
        word: '',
        replaceType: '',
        replacement: ''
      };
      this.dialogVisible = true;
    },
    handleEdit(row) {
      this.dialogType = 'edit';
      this.form = { ...row };
      this.dialogVisible = true;
    },
    handleDelete(row) {
      this.$confirm('确认删除该违禁词吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功'
        });
      });
    },
    handleSubmit() {
      this.$message({
        type: 'success',
        message: this.dialogType === 'add' ? '添加成功' : '修改成功'
      });
      this.dialogVisible = false;
    },
    handleImport() {
      this.$message({
        type: 'success',
        message: '批量导入成功'
      });
    },
    handleExport() {
      this.$message({
        type: 'success',
        message: '导出成功'
      });
    },
    handleCurrentChange(page) {
      this.currentPage = page;
    },
    handleSizeChange(size) {
      this.pageSize = size;
    }
  }
};
</script>

<style scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* 表格样式优化 */
.el-table .el-table__cell {
  padding: 8px 0; /* 减少单元格内边距 */
}

.el-table .el-table__header th {
  padding: 8px 0; /* 减少表头内边距 */
}

.el-table .el-button--text {
  padding: 0; /* 减少按钮内边距 */
}
</style>